package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-09 09:11
 * @description
 */
@Service
/**
 * @Transactional  这个类方法 都需要事务控制!!
 *  我们观察一下 这个 service中 到底只读的多 还是 非只读事务的多
 *
 *
 *
 */
@Transactional(readOnly = true)
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;


    @Override
    @Transactional(readOnly = false)
    public void transfer(String from, String to, double money) {

        //给from减少
        accountDao.sub(from,money);

        int i=5/0;

        //写了方法 需要很长时间才能运行完
        //给to增加
        accountDao.add(to,money);

    }

    @Override

    public Account findById(int id) {
        return accountDao.findById(id);
    }


}
